844B - Rectangles - CodeForces Solution


combinatorics math *1300

Please click on ads to support us..

Python Code:

from operator import mul    from fractions import Fraction
from functools import reduce

sol_super_comb = {i:0 for i in range(51)}

def retangles():
  n, m = map(int, input().split())
  vectors = [[[0]*n, [0]*m], [[0]*n, [0]*m]]
  maior = max(n,m)

  [[summs(vectors, row, col, 1) if x=='1' else summs(vectors, row, col, 0) for col,x in enumerate(input().split())] for row in range(n)]

  soma=n*m
  for elem_one_zero in vectors:
    for row_or_col in elem_one_zero:
      for i in row_or_col:
        if(i>1):
          soma+=super_comb(i)
  print(soma)

def summs(v, r, c, e):
  v[e][0][r]+=1
  v[e][1][c]+=1
  return 0

def super_comb(n):
  if(sol_super_comb[n]>0):
    return sol_super_comb[n]
  soma = 0
  for i in range(n):
    if(i>=1):
      soma+=comb(n,i+1)
  sol_super_comb[n] = soma
  return soma

  
def comb(n,k): 
  return int( reduce(mul, (Fraction(n-i, i+1) for i in range(k)), 1) )
retangles()
			   				   	 	 	 					 		  		

C++ Code:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> a(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int cntBlack = 0;
        for (int j = 0; j < m; j++) {
            cntBlack += a[i][j];
        }
        ans += (1ll << cntBlack) - 1;
        ans += (1ll << (m - cntBlack)) - 1;
    }

    for (int j = 0; j < m; j++) {
        int cntBlack = 0;
        for (int i = 0; i < n; i++) {
            cntBlack += a[i][j];
        }
        ans += (1ll << cntBlack) - 1;
        ans += (1ll << (n - cntBlack)) - 1;
    }

    cout << ans - n * m;
}


Comments

Submit
0 Comments
More Questions

1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers